package com.yangyouqi.posthorse.admin.controller;

import com.yangyouqi.posthorse.admin.from.RegisterFrom;
import com.yangyouqi.posthorse.core.domain.Customer;
import com.yangyouqi.posthorse.core.domain.Manager;
import com.yangyouqi.posthorse.core.service.ManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author 杨友奇
 * @Time 2018/7/14
 * 管理员登录
 */

@Controller
@RequestMapping("publiz")
public class PublizController {

    @Autowired
    private ManagerService managerService;

    //进入注册页面
    @GetMapping("/register")
    public String register() {
        return "publiz/register";
    }

    //执行注册操作
    @PostMapping("register")
    public String register(@Validated RegisterFrom registerFrom, BindingResult result1,
                           Model model) {
        //进行判断用户输入是否非法
        if (result1.hasErrors()) {
            List<ObjectError> list = result1.getAllErrors();
            for (ObjectError error : list) {
                System.out.println(error.getDefaultMessage());
                model.addAttribute("errorMsg", error.getDefaultMessage());
                return "publiz/register";
            }
        }
        //进行判断数据库中是否存在该用户
        if (managerService.existsusername(registerFrom.getUsername())) {
            model.addAttribute("errorMsg", "用户已存在");
            return "publiz/register";
        } else {
            //进行注册
            Manager manager = new Manager(registerFrom.getUsername(), registerFrom.getNickname(),
                    registerFrom.getPassword());

            Manager result = managerService.register(manager);
            if (result != null) {
                //注册成功,跳转到登录界面，并携带相关提示信息
                model.addAttribute("manager", manager);
                return "publiz/regsuccess";
            } else {
                //注册失败
                return "publiz/register";
            }
        }

    }


    @GetMapping("/login")
    public String login(){
        return "/publiz/login";
    }
    @PostMapping("/login")
    public String login(@RequestParam String username, @RequestParam String password, Model model, HttpSession session){
       Manager manager =  managerService.login(username, password);
       if (manager != null){
           //存储回话信息
           session.setAttribute("manager",manager);
           return "redirect:/order/index";
       }else {
           model.addAttribute("errorMsg","账户或者密码错误");
           return "publiz/login";
       }
    }

    /**
     * 退出登录
     * @return
     */
    @GetMapping("/logout")
    public String logout(HttpSession session){
        session.removeAttribute("manager");
        //重定向到订单的首页
        return "redirect:/publiz/login";
    }

    private String getErrorMessage(BindingResult bindingResult){
        StringBuilder errorMsg = new StringBuilder();
        int i = 0;
        for (ObjectError error : bindingResult.getAllErrors()) {
            if (i != 0) {
                errorMsg.append("<br/>");
            }
            errorMsg.append(error.getDefaultMessage());
            i++;
        }
        return errorMsg.toString();
    }
}
